﻿
@{
    Layout = null;
}

<div ng-controller="DrawSetAddressCtrl">
    <div ng-show="mainShow">
        <div class="w_oderDetail">
            <div class="w_odt_info" ng-show="Adress.Id>0" ng-click="showAddress()">
                <div class="w_odtInfo_a">
                    <span>{{Adress.Consignee}}</span>
                    <label>{{Adress.Mobile}}</label>
                </div>
                <p class="w_odtInfo_b">{{Adress.ProvinceName}}{{Adress.CityName}}{{Adress.DistrictName}} {{Adress.Address}}</p>
                <img src="/Content/images/w_05.png" class="w_odtl_gt" />
            </div>
            <div class="w_odt_info" ng-show="!(Adress.Id>0)" ng-click="showAddress()">
                <a class="w_quickRob_topa">新增地址<img src="/Content/images/w_05.png" class="w_odtl_gt" /></a>
                <p class="w_quickRob_topb">请准确填写你常用地址，避免送错</p>
            </div>
            <div class="w_odt_pro w_quickRob_pro">
                <!--<div class="w_odt_proti">优品三悦</div>-->
                <div class="w_odt_procon f_w_odt">
                    <div class="w_odtpro_conleft"><img src="{{ImgHostUrl+Product.ImgUrl}}" /></div>
                    <div class="w_odtpro_conright">
                        <div class="w_odtpro_name2">{{Product.Name}}</div>
                        <p class="w_odtpro_more">
                            <label>X<big>1</big></label>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <!--底部-->
        <div class="w_jfxqbot_fixed">
            <a ng-click="submit()" class="w_jfxqbot_right">提交</a>
        </div>
    </div>
    <div ng-show="adressShow">
        <div class="fual_r" ng-repeat="item in List">
            <div class="uai_m">
                <p class="uinp">
                    <span>{{item.Consignee}}</span>
                    <label>{{item.Mobile}}</label>
                </p>
                <p>{{item.Address}}</p>
            </div>
            <div class="uied">
                <label ng-class="{true:'fuidef isdefault',false:'fuidef'}[item.Id==Address.Id]" ng-click="choseAddress(item)">
                    选择
                </label>

                <a class="addrdel" ng-click="delAddress(item.Id)">删除</a>
                <a ng-click="editAddress(item);" class="addredit">编辑</a>
            </div>
        </div>
        <button class="fscibtn" style="bottom:60px" ng-click="cancel()">取消</button>
        <button class="fscibtn" ng-click="showAdressInfo()">新建地址</button>
    </div>

    <div ng-show="adressInfoShow">
        <div class="fsci_b">
            <div class="fsci_r">
                <span class="sci_l">收货人</span>
                <input type="text" placeholder="请输入收货人姓名" name="Consignee" id="Consignee" ng-model="Model.Consignee" class="sci_r">
            </div>
            <div class="fsci_r">
                <span class="sci_l">手机号</span>
                <input type="text" placeholder="请输入收货人手机号码" id="Mobile" name="Mobile" ng-model="Model.Mobile" class="sci_r">
            </div>
            <div class="f_bg_w">
                <section class="f_bd1 f_are">
                    <a href="javascript:void(0)" ng-click="AreaShow()">
                        <dl>
                            <dt class="f_l f_fz14">所在地</dt>
                            <dd class="f_r f_rColor selectCity f_fz14" id="expressArea">
                                <span ng-show="!(Model.District>0)">请选择<img src="/Content/images/goto_c.png" class="f_ssqimg2"></span>
                                <label ng-show="Model.District>0">{{Model.ProvinceName}} {{Model.CityName}} {{Model.DistrictName}}</label>
                            </dd>
                        </dl>
                    </a>
                </section>
                <textarea class="f_addDT" placeholder="请输入详细地址" id="Address" name="Address" ng-model="Model.Address"></textarea>
            </div>
        </div>
        <!--end 收货人信息 -->
        <!-- 是否设为默认地址 -->
        <div class="f_mt10 f_bg_w fscir2" ng-click="setIsDefault()">
            <label for="isdefultaddr" ng-class="{true:'fscir_is_default isdefault',false:'fscir_is_default'}[Model.IsDefault]">设为默认地址</label>
        </div>
        <!-- end 是否设为默认地址 -->
        <!-- 按钮 -->
        <button class="fscibtn" style="bottom:60px" ng-click="cancel()">取消</button>
        <button class="fscibtn" ng-click="saveAddress()">保存</button>

        <!-- end 按钮 -->
        <!--地址选择-->
        <div class="w_addPopall">
            <div id="areaMask" class="mask" ng-show="showArea" ng-click="AreaHide()"></div>
            <div class="w_addPop" id="areaLayer" ng-show="showArea">
                <div class="w_addPop_ti">收货地址<p class="w_popclose22" ng-click="AreaHide()"><img src="/Content/images/w2_22.png" class="w_addPopclose" /><img src="/Content/images/w2_23.png" class="w_addPopLine" /></p></div>
                <!--<ul class="w_addPop_menu" id="expressArea">
                    <li>江苏</li>
                    <li id="expres2">江苏</li>
                    <li>江苏</li>
                </ul>-->
                <div class="f_poplist" id="areaBox">
                    <ul class="f_popul" ng-show="level1">
                        <li ng-repeat="item in Province" ng-click="cp(item)">
                            {{item.Name}}
                        </li>

                    </ul>
                    <ul class="f_popul" ng-show="level2">
                        <li ng-repeat="item in City" ng-click="cc(item)">
                            {{item.Name}}
                        </li>
                    </ul>
                    <ul class="f_popul" ng-show="level3">
                        <li ng-repeat="item in District" ng-click="cd(item)">
                            {{item.Name}}
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <!--地址选择-->
    </div>
</div>

<script>
    App.controller('DrawSetAddressCtrl', [
                '$scope', '$state', '$route', '$http', '$rootScope', '$stateParams',
                    function ($scope, $state, $route, $http, $rootScope , $stateParams) {
                        $scope.level1 = true;
                        $scope.level2 = false;
                        $scope.level3 = false;
                        $scope.showArea = false;
                        $scope.adressShow = false;
                        $scope.adressInfoShow = false;
                        $scope.mainShow = true;
                        $scope.couponShow = false;
                        $scope.IsUseCoupon = false;
                        $scope.IsUsePoint = false;
                        $scope.Model = {};
                        $scope.Record = {};

                        $scope.IsUseBalance = false;
                        var tt = function () {
                            $rootScope.WxShare($rootScope.pageTitle(), '优品三悦', location.href, $rootScope.LogoUrl);
                        }

                        setTimeout(tt, 1000)
                        $http.post("/UserCenter/GetDefaultAddress").then(function (response) {
                            $scope.Adress = angular.copy(response.data.data);
                            if ($scope.Adress == null) {
                                $scope.Adress = {};
                                $scope.Adress.Province = 0;
                            }


                            //获取商品信息
                            if ($stateParams.Id != null && $stateParams.Id != "") {
                                $http.post("/Draw/GetProduct", { id: $stateParams.Id }).then(function (response) {
                                    if (response.data.isSuccess) {
                                        $scope.Product = angular.copy(response.data.data);
                                    }
                                });
                            }

                        });

                        $http.post("/Order/GetAddressList").then(function (response) {
                            $scope.List = angular.copy(response.data.data);
                            if ($scope.List == null) {
                                $scope.List = [];
                            }
                        });

                        $scope.choseAddress = function (adress) {
                            $scope.Adress = adress;
                            $scope.adressShow = false;
                            $scope.adressInfoShow = false;
                            $scope.mainShow = true;
                        }

                        $scope.showAddress = function () {
                            $scope.adressShow = true;
                            $scope.adressInfoShow = false;
                            $scope.mainShow = false;
                        }

                        $scope.showAdressInfo = function () {
                            $scope.Model = {};
                            $scope.adressShow = false;
                            $scope.adressInfoShow = true;
                            $scope.mainShow = false;
                        }

                        $scope.editAddress = function (item) {
                            $scope.Model = item;
                            $scope.adressShow = false;
                            $scope.adressInfoShow = true;
                            $scope.mainShow = false;
                        }

                        $scope.delAddress = function (id) {
                            layer.confirm('确定删除吗？', {
                                btn: ['确定', '取消'] //按钮
                            }, function () {
                                layer.load(2);
                                $http.post("/UserCenter/DelAddress", { id: id }).then(function (response) {

                                    if (response.data.isSuccess) {
                                        layer.msg("删除成功");
                                        if ($scope.Adress.Id == id) {
                                            $scope.Adress = {};
                                        }
                                        $http.post("/UserCenter/GetMyAddress").then(function (response) {
                                            layer.closeAll('loading');
                                            $scope.List = angular.copy(response.data.data);
                                            if ($scope.List == null) {
                                                $scope.List = {};
                                            }
                                        });
                                    }
                                    else {
                                        layer.closeAll('loading');
                                        layer.msg(response.data.msg);
                                    }
                                });
                            }, function () {

                            });
                        }

                        //获取省级区域
                        $http.post("/UserCenter/GetArea", { pid: 0 }).then(function (response) {
                            $scope.Province = angular.copy(response.data.data);
                            if ($scope.Province == null) {
                                $scope.Province = {};
                            }
                        });

                        $scope.AreaShow = function () {
                            $scope.level1 = true;
                            $scope.level2 = false;
                            $scope.level3 = false;
                            $scope.showArea = true;
                        }

                        $scope.AreaHide = function () {
                            $scope.showArea = false;
                        }

                        $scope.cp = function (item) {
                            $scope.Model.ProvinceName = item.Name;
                            $scope.Model.Province = item.Id;
                            $scope.Model.City = 0;
                            $scope.Model.District = 0;
                            $http.post("/UserCenter/GetArea", { pid: item.Id }).then(function (response) {
                                $scope.City = angular.copy(response.data.data);
                                if ($scope.City == null) {
                                    $scope.City = {};
                                }
                                $scope.level1 = false;
                                $scope.level2 = true;
                                $scope.level3 = false;
                            });
                        }

                        $scope.cc = function (item) {
                            $scope.Model.CityName = item.Name;
                            $scope.Model.City = item.Id;
                            $scope.Model.District = 0;
                            $http.post("/UserCenter/GetArea", { pid: item.Id }).then(function (response) {
                                $scope.District = angular.copy(response.data.data);
                                if ($scope.District == null) {
                                    $scope.District = {};
                                }
                                $scope.level1 = false;
                                $scope.level2 = false;
                                $scope.level3 = true;
                            });
                        }

                        $scope.cd = function (item) {
                            $scope.Model.DistrictName = item.Name;
                            $scope.Model.District = item.Id;
                            $scope.showArea = false;
                        }

                        $scope.setIsDefault = function () {
                            if ($scope.Model.IsDefault) {
                                $scope.Model.IsDefault = false;
                                return;
                            }
                            else {
                                $scope.Model.IsDefault = true;
                                return;
                            }
                        }


                        $scope.saveAddress = function () {
                            if ($scope.Model.Consignee == undefined || $scope.Model.Consignee == "") {
                                layer.msg("请输入收货人姓名");
                                return false;
                            }
                            if ($scope.Model.Mobile == undefined || $scope.Model.Mobile == "") {
                                layer.msg("请输入收货人手机号");
                                return false;
                            }
                            if (!($scope.Model.District > 0)) {
                                layer.msg("请选择所在地");
                                return false;
                            }

                            if ($scope.Model.Address == undefined || $scope.Model.Address == "") {
                                layer.msg("请输入详细地址");
                                return false;
                            }
                            layer.load(2);
                            $http.post("/UserCenter/SaveAddress", { model: $scope.Model }).then(function (response) {
                                layer.closeAll('loading');
                                if (response.data.isSuccess) {
                                    $http.post("/Order/GetAddressList").then(function (response) {
                                        console.log(response.data.data);

                                        $scope.List = angular.copy(response.data.data);
                                        $scope.adressInfoShow = false;
                                        $scope.adressShow = true;
                                        $scope.mainShow = false;

                                    });

                                }
                                else {
                                    layer.msg(response.data.msg);
                                }
                            });
                        }

                        $scope.cancel = function () {
                            $scope.adressInfoShow = false;
                            $scope.adressShow = false;
                            $scope.mainShow = true;
                        }


                        $scope.submit = function () {
                            if ($scope.Adress == null || $scope.Adress == undefined || angular.equals({}, $scope.Adress)) {
                                layer.msg("请选择地址");
                            }
                            $scope.Record.Id = $stateParams.Id;
                            
                            $scope.Record.Consignee = $scope.Adress.Consignee;
                            $scope.Record.Mobile = $scope.Adress.Mobile;
                            $scope.Record.Province = $scope.Adress.Province;
                            $scope.Record.City = $scope.Adress.City;
                            $scope.Record.District = $scope.Adress.District;
                            $scope.Record.Address = $scope.Adress.Address;
                            $scope.Record.Zipcode = $scope.Adress.Zipcode;

                            layer.load(2);
                            $http.post("/Draw/SubmitAddress", { record: $scope.Record }).then(function (response) {
                                layer.closeAll('loading');
                                if (response.data.isSuccess) {
                                    layer.msg("提交成功");
                                    $state.go('draw.list');
                                }
                                else {
                                    layer.msg(response.data.msg);
                                }
                            });
                        }
                    }
    ]);
</script>
